/*     */ package com.zimbra.cs.pop3;
/*     */ 
/*     */ import com.zimbra.common.io.TcpServerInputStream;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.NetUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.security.sasl.Authenticator;
/*     */ import com.zimbra.cs.server.ProtocolHandler;
/*     */ import java.io.BufferedOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStream;
/*     */ import java.net.InetAddress;
/*     */ import java.net.InetSocketAddress;
/*     */ import java.net.Socket;
/*     */ import javax.net.ssl.SSLSession;
/*     */ import javax.net.ssl.SSLSocket;
/*     */ import javax.net.ssl.SSLSocketFactory;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class TcpPop3Handler
/*     */   extends ProtocolHandler
/*     */ {
/*     */   private TcpServerInputStream input;
/*     */   private String remoteAddress;
/*     */   private final HandlerDelegate delegate;
/*     */   
/*     */   TcpPop3Handler(TcpPop3Server server)
/*     */   {
/*  39 */     super(server);
/*  40 */     this.delegate = new HandlerDelegate(server.getConfig());
/*     */   }
/*     */   
/*     */   protected boolean setupConnection(Socket connection) throws IOException
/*     */   {
/*  45 */     this.remoteAddress = connection.getInetAddress().getHostAddress();
/*  46 */     this.input = new TcpServerInputStream(connection.getInputStream());
/*  47 */     this.delegate.output = new BufferedOutputStream(connection.getOutputStream());
/*  48 */     if (this.delegate.startConnection(connection.getInetAddress())) {
/*  49 */       return true;
/*     */     }
/*  51 */     dropConnection();
/*  52 */     return false;
/*     */   }
/*     */   
/*     */   protected boolean processCommand()
/*     */     throws IOException
/*     */   {
/*  58 */     setIdle(false);
/*  59 */     if (this.delegate.processCommand(this.input.readLine())) {
/*  60 */       return true;
/*     */     }
/*  62 */     dropConnection();
/*  63 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   protected void dropConnection()
/*     */   {
/*  69 */     ZimbraLog.addIpToContext(this.remoteAddress);
/*     */     try {
/*  71 */       if (this.input != null) {
/*  72 */         this.input.close();
/*  73 */         this.input = null;
/*     */       }
/*  75 */       if (this.delegate.output != null) {
/*  76 */         this.delegate.output.close();
/*  77 */         this.delegate.output = null;
/*     */       }
/*     */     } catch (IOException e) {
/*  80 */       if (ZimbraLog.pop.isDebugEnabled()) {
/*  81 */         ZimbraLog.pop.debug("I/O error while closing connection", e);
/*     */       } else {
/*  83 */         ZimbraLog.pop.debug("I/O error while closing connection: " + e);
/*     */       }
/*     */     } finally {
/*  86 */       ZimbraLog.clearContext();
/*     */     }
/*     */   }
/*     */   
/*     */   protected boolean authenticate()
/*     */     throws IOException
/*     */   {
/*  93 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */   protected void notifyIdleConnection()
/*     */   {
/*  99 */     ZimbraLog.pop.debug("idle connection");
/*     */   }
/*     */   
/*     */   private class HandlerDelegate extends Pop3Handler
/*     */   {
/*     */     HandlerDelegate(Pop3Config config) {
/* 105 */       super();
/*     */     }
/*     */     
/*     */     protected void startTLS() throws IOException
/*     */     {
/* 110 */       sendOK("Begin TLS negotiation");
/* 111 */       SSLSocketFactory fac = (SSLSocketFactory)SSLSocketFactory.getDefault();
/* 112 */       SSLSocket sock = (SSLSocket)fac.createSocket(TcpPop3Handler.this.connection, TcpPop3Handler.this.connection.getInetAddress().getHostName(), TcpPop3Handler.this.connection.getPort(), true);
/*     */       
/* 114 */       NetUtil.setSSLProtocols(sock, this.config.getMailboxdSslProtocols());
/* 115 */       NetUtil.setSSLEnabledCipherSuites(sock, this.config.getSslExcludedCiphers(), this.config.getSslIncludedCiphers());
/* 116 */       sock.setUseClientMode(false);
/* 117 */       TcpPop3Handler.this.startHandshake(sock);
/* 118 */       ZimbraLog.pop.debug("suite: %s", new Object[] { sock.getSession().getCipherSuite() });
/* 119 */       TcpPop3Handler.this.input = new TcpServerInputStream(sock.getInputStream());
/* 120 */       this.output = new BufferedOutputStream(sock.getOutputStream());
/*     */     }
/*     */     
/*     */     protected void completeAuthentication() throws IOException
/*     */     {
/* 125 */       setLoggingContext();
/* 126 */       this.authenticator.sendSuccess();
/* 127 */       if (this.authenticator.isEncryptionEnabled())
/*     */       {
/* 129 */         TcpPop3Handler.this.input = new TcpServerInputStream(this.authenticator.unwrap(TcpPop3Handler.this.connection.getInputStream()));
/* 130 */         this.output = this.authenticator.wrap(TcpPop3Handler.this.connection.getOutputStream());
/*     */       }
/*     */     }
/*     */     
/*     */     InetSocketAddress getLocalAddress()
/*     */     {
/* 136 */       return (InetSocketAddress)TcpPop3Handler.this.connection.getLocalSocketAddress();
/*     */     }
/*     */     
/*     */     void sendLine(String line, boolean flush) throws IOException
/*     */     {
/* 141 */       ZimbraLog.pop.trace("S: %s", new Object[] { line });
/* 142 */       this.output.write(line.getBytes());
/* 143 */       this.output.write(LINE_SEPARATOR);
/* 144 */       if (flush) {
/* 145 */         this.output.flush();
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/pop3/TcpPop3Handler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */